#include "Thread.hpp"
#include <unordered_map>
#include <memory>
#include <vector>

struct ThreadData
{
    int _start = 0;
    int _end = 0;
    int _res = 0;
    ThreadData(int start, int end)
    :_start(start),_end(end)
    {}
};

void count(ThreadData* td)
{
    int sum = 0;
    for (int i = td->_start; i <= td->_end; i++)
    {
        sum += i;
    }
    td->_res = sum;
}

using thread_ptr_t = std::shared_ptr<ThreadModule::Thread<ThreadData>>;
const static int NUM = 10;

int main()
{
    // 创建多线程
    std::unordered_map<std::string, thread_ptr_t> threads;  // 哈希表映射线程名和智能指针
    std::vector<ThreadData*> v;
    for (int i = 0; i < NUM; i++)
    {
        v.push_back(new ThreadData(i, i*100));
        thread_ptr_t t = std::make_shared<ThreadModule::Thread<ThreadData>>(count, v[i]);
        threads[t->Name()] = t;
    }

    for (const auto& e : threads)
    {
        e.second->Start();
    }

    for (const auto& e: threads)
    {
        e.second->Join();
    }

    for (const auto& e : v)
    {
        printf("%d+...+%d=%d\n", e->_start, e->_end, e->_res);
    }
    for (auto e : v)
    {
        delete e;
    }
    return 0;
}